package com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.repository;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iwhalecloud.citybrain.flow.platform.manager.application.utils.StringUtils;
import com.iwhalecloud.citybrain.flow.platform.manager.common.ResponseResult;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.enums.NoticeTypeEnum;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.User;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.comment.*;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowCommentConfigRepository;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.NoticeRepository;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.UserRepository;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.entities.FlowCommentConfig;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.entities.FlowCommentConfigExample;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.mapper.FlowCommentConfigDAO;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.mapper.NoticeTemplateDAO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 审批意见配置Service
 *
 * @author liuhuimin
 * @date 2021-11-02
 */
@Service
public class FlowCommentConfigRepositoryImpl implements FlowCommentConfigRepository {

    @Autowired
    private FlowCommentConfigDAO flowCommentConfigDAO;

    @Autowired
    private UserRepository userRepository;


    private final static String SEND_SUCCESS = "Y";

    @Override
    public ResponseResult list(FlowCommentConfigQueryVo flowCommentConfigQueryVo) {
        FlowCommentConfigExample flowCommentConfigExample = new FlowCommentConfigExample();
        if (StringUtils.isNotEmpty(flowCommentConfigQueryVo.getSearchText())) {
            String searchText = "%" + flowCommentConfigQueryVo.getSearchText() + "%";
            flowCommentConfigExample.createCriteria().andUserIdLike(searchText);
            flowCommentConfigExample.or().andFlowNameLike(searchText);
            flowCommentConfigExample.or().andContentLike(searchText);
            flowCommentConfigExample.or().andTypeEqualTo(searchText);
        }
        PageHelper.startPage(flowCommentConfigQueryVo.getPageNum(), flowCommentConfigQueryVo.getPageSize());
        List<FlowCommentConfig> flowCommentConfigs = flowCommentConfigDAO.selectByExample(flowCommentConfigExample);
        PageInfo<FlowCommentConfig> pageInfo = new PageInfo<>(flowCommentConfigs);

        ResponseResult responseResult = ResponseResult.success();
        responseResult.setTotalCount((int) pageInfo.getTotal());
        responseResult.setPageNum(flowCommentConfigQueryVo.getPageNum());
        responseResult.setPageSize(flowCommentConfigQueryVo.getPageSize());
        responseResult.setDatas(flowCommentConfigs);
        return responseResult;
    }


    @Override
    public Boolean isTemplateExist(String noticeType) {
//        FlowCommentConfigExample flowCommentConfigExample = new FlowCommentConfigExample();
//        flowCommentConfigExample.createCriteria().andTypeEqualTo(noticeType).andStatusEqualTo();
//        List<FlowCommentConfig> flowCommentConfigs = flowCommentConfigDAO.selectByExample(flowCommentConfigExample);
//        if (CollectionUtils.isEmpty(flowCommentConfigs)) {
//            return false;
//        }
        return true;
    }
    @Override
    public Boolean isTemplateExist(String noticeType,Long id) {
//        FlowCommentConfigExample flowCommentConfigExample = new FlowCommentConfigExample();
//        flowCommentConfigExample.createCriteria().andTypeEqualTo(noticeType).andStatusEqualTo().andIdNotEqualTo(id);
//        List<FlowCommentConfig> flowCommentConfigs = flowCommentConfigDAO.selectByExample(flowCommentConfigExample);
//        if (CollectionUtils.isEmpty(flowCommentConfigs)) {
//            return false;
//        }
        return true;
    }

    @Override
    public Long insert(FlowCommentConfigAddVo flowCommentConfigAddVo) {
        FlowCommentConfig flowCommentConfig = new FlowCommentConfig();
        BeanUtils.copyProperties(flowCommentConfigAddVo, flowCommentConfig);
        User current = userRepository.getCurrentUser();
        if(null!=current) {
            flowCommentConfig.setUserId(current.getUserCode());
            flowCommentConfig.setCreateBy(current.getUserName());
        }
        flowCommentConfig.setCreateTime(new Date());
        flowCommentConfigDAO.insert(flowCommentConfig);
        return flowCommentConfig.getId();
    }

    @Override
    public Integer update(FlowCommentConfigUpdateVo flowCommentConfigUpdateVo) {
        FlowCommentConfig flowCommentConfig = new FlowCommentConfig();
        BeanUtils.copyProperties(flowCommentConfigUpdateVo, flowCommentConfig);
        flowCommentConfig.setUpdateTime(new Date());
        return flowCommentConfigDAO.updateByPrimaryKey(flowCommentConfig);
    }

    @Override
    public Integer delete(FlowCommentConfigDeleteVo flowCommentConfigDeleteVo) {
        return flowCommentConfigDAO.deleteByPrimaryKey(flowCommentConfigDeleteVo.getId());
    }




    @Override
    public ResponseResult detail(FlowCommentConfigDetailVo flowCommentConfigDetailVo) {
        FlowCommentConfig flowCommentConfig = flowCommentConfigDAO.selectByPrimaryKey(flowCommentConfigDetailVo.getId());
        return ResponseResult.success(flowCommentConfig);
    }

}
